package com.ls.fw.data.result;

import java.util.ArrayList;
import java.util.List;

import com.ls.fw.data.bean.Column;
import com.ls.fw.data.bean.Record;
import com.ls.fw.data.visitor.RecordVisitor;

/**
 * 查询结果
 * @author ls
 * 2014年11月21日下午2:44:50
 */
public class DataResult implements Cloneable{
	
	private long total = 0L;
	
	private Float maxScore = 0F;

	private List<Record> results = new ArrayList<Record>();

	
	public DataResult() {
		super();
	}
	
	public DataResult(long total) {
		super();
		this.total = total;
	}
 
	public boolean add(Record record){
		return results.add(record);
	}
	
	
	public List<Record> getResults() {
		return results;
	}

	
	public void setResults(List<Record> results) {
		this.results = results;
	}

	
	public long getTotal() {
		return total;
	}

	public void setTotal(long total) {
		this.total = total;
	}

	public Float getMaxScore() {
		return maxScore;
	}

	public void setMaxScore(Float maxScore) {
		this.maxScore = maxScore;
	}

	@Override
	public String toString() {
		return "DataResult [total=" + total + ", maxScore=" + maxScore
				+ ", results=" + results + "]";
	}

	/**
	 * 输出表结构
	 * @author ls
	 * 2014年11月24日下午1:49:14 
	 * @return
	 */
	public String printTable(){
		StringBuilder sb = new StringBuilder();
		sb.append("total="+total).append(",").append("maxScore="+maxScore).append("\n");
		if(!results.isEmpty()){
			Record record = results.get(0);
			List<Column> fs = record.getFields();
			sb.append("序号");
			for (Column column : fs) {
				sb.append(" | ");
				sb.append(column.getShowName());
			}
			sb.append("\n");
			int index1 = 0;
			for (Record record1 : results) {
				List<Column> fs1 = record1.getFields();
				sb.append(++index1);
				for (Column column : fs1) {
					sb.append(" | ");
					sb.append(column.getValue());
				}
				sb.append("\n");
			}
		}
		return sb.toString();
	}
	
	public int accept(RecordVisitor recordVisitor) {
		if(this.results != null && !results.isEmpty()){
			for (Record field : results) {
				recordVisitor.visit(field);
			}
			return results.size();
		}
		return 0;
	}
	
	public boolean hasRecord(){
		return (results!=null && !results.isEmpty())?false:true;
	}
	
	public int getSize(){
		return (results!=null && !results.isEmpty())?results.size():0;
	}
}
